package reviewAlgorithms.search;

import java.util.ArrayList;
import java.util.List;

public class Combine {
    public static void main(String[] args) {
        int n = 4;
        int k = 3;
        List<List<Integer>> res = combine(n,k);
        for (List<Integer> item: res){
            System.out.println(item);
        }
    }
    public static List<List<Integer>> combine(int n, int k) {
        List<List<Integer>> res = new ArrayList<>();
        if (n < 1 || n < k){
            return res;
        }
        List<Integer> oneRes = new ArrayList<>();
        int start = 1;
        int index = 1;
        getCombine(n,start,k, index, res,oneRes);
        return res;
    }

    private static void getCombine(int n, int start, int k, int index, List<List<Integer>> res, List<Integer> oneRes) {
        if (start > k){
            res.add(new ArrayList<>(oneRes));
            return;
        }

        for (int i = index; i <= n -  (k - start); i++) {
            oneRes.add(i);
            getCombine(n,start + 1,k ,i + 1,res,oneRes);
            oneRes.remove(oneRes.size() - 1);
        }
    }
}
